package com.simple.apt.processor;

import cn.hutool.core.text.StrFormatter;
import cn.hutool.core.util.StrUtil;
import com.google.auto.service.AutoService;
import com.simple.annotation.apt.Simpler;

import javax.annotation.processing.Processor;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.element.TypeElement;

/**
 * @author FanXing
 * @date 2023年01月25日 0:35
 */
@SupportedAnnotationTypes({"com.simple.annotation.apt.Simpler"})
@AutoService(Processor.class)
public class ServiceProcessor extends BasicProcessor {
    private static final String PATTERN =
            "package {};\n" +
            "\n" +
            "import com.simple.core.service.IService;\n" +
            "\n" +
            "public interface {} extends IService<{}> {\n" +
            "}";

    @Override
    protected String initBuilderClassSrc(TypeElement annotatedClass, String packageStr, String builderClassName, String entityClassName) {
        return StrFormatter.format(PATTERN, packageStr, builderClassName, entityClassName);
    }

    @Override
    protected String initBuilderClassName(TypeElement annotatedClass, String className) {
        String actual = annotatedClass.getAnnotation(Simpler.class).prefix();
        String prefix = className;
        if (StrUtil.isNotBlank(actual)) prefix = actual;
        return prefix + "Service";
    }
}
